<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

class DemoController extends Controller
{
    public function add(Request $request){
        echo "当前URL：".$request->path()."<br/>";
        echo "当前URL:".$request->url()."<br/>";
        echo "当前请求的方法：".$request->method()."<br/>";
        echo "获取请求参数name的值：".$request->input('name')."<br/>";
        echo "获取请求参数name的值：".$request->name."<br/>";
        echo "获取请求参数name的值:".$request->input('name','没有值')."<br/>";
        echo "获取session的值：".session("uuname")."<br/>";
        //判断是否有name的参数
        if($request->has('name')){
            
        }
        //获取所有参数
        $data = $request->all();
        print_r($data);
        return "加载添加表单";
    }
    
    //测试文件上传代码
    public function doUpload(Request $request)
    {
        //var_dump($_FILES);
        //使用自定义文件上传类处理上传
        $upfile = new \App\Org\Upload("ufile");
        //初始化上传信息
        $upfile->path = "./uploads/";  //上传存储路径
        $upfile->typeList = array("image/jpeg","image/png","image/gif");  //设置允许上传类型
        $upfile->maxSize = 0;  //允许上传大小
        
        //执行文件上传
        $res = $upfile->doUpload();
        
        //判断输出
        if($res){
             return "上传成功！".$upfile->saveName;
        }else{
            return "上传失败！原因：".$upfile->error; 
        }
    }
    
    
    //响应实例
    public function response()
    {
//        return "ok";
        //附加cookie信息的响应设置
        //return response("hello",200)->withCookie("login","张三丰");
        
        //响应json格式
        $data = [['name'=>'张无忌','age'=>22,'sex'=>'男'],['name'=>'张翠山','age'=>20,'sex'=>'女']];
        return response()->json($data);
        
        //响应下载(需要在php.ini中开启extension=php_fileinfo.dll模块)
       // return response()->download("./uploads/a.txt");
        
        //重定向
        //return redirect('stu/create');
        
        return redirect('/demo2/add')->with('uuname','zhansanfeng');
    }
    
    
    //视图实例
    public function view()
    {
        return view('myview',['name'=>'lizi','info'=>'<b>BBBBBB</b><i>IIIIIIII</i>']);
    }
}
